package com.fastDFS;

import com.anniweiya.fastdfs.FastDFSTemplateFactory;
import com.anniweiya.fastdfs.pool.PoolConfig;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * com.cimr.icloud.common.fastDFS.config
 *
 * @Auther: caiming
 * @Date: 2018/9/29 14:22
 * @Description:
 */
@Configuration
@PropertySource("classpath:config/fdfs_client.properties")
public class FastDFSConfig {

    @Value("${connect_timeout}")
    private int g_connect_timeout;
    @Value("${network_timeout}")
    private int g_network_timeout;
    @Value("${poolConfig.maxIdle}")
    private Integer maxIdle;
    @Value("${poolConfig.maxTotal}")
    private Integer maxTotal;
    @Value("${charset}")
    private String g_charset;
    @Value("${http.tracker_http_port}")
    private int g_tracker_http_port;
    @Value("${http.anti_steal_token}")
    private boolean g_anti_steal_token;
    @Value("${http.secret_key}")
    private String g_secret_key;
    @Value("${tracker_server}")
    private String tracker_server;

    public static TrackerClient tracker = null;
    public static TrackerServer trackerServer = null;
    public static StorageServer storageServer = null;

    public static String[] tracker_server_address = null;

    @Bean
    public PoolConfig poolConfig() {
        PoolConfig poolConfig = new PoolConfig();
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMaxIdle(maxIdle);
        return poolConfig;
    }

    @Bean
    public FastDFSTemplateFactory fastDFSTemplateFactory(PoolConfig poolConfig) throws Exception {
        FastDFSTemplateFactory fastDFSTemplateFactory = new FastDFSTemplateFactory();
        fastDFSTemplateFactory.setPoolConfig(poolConfig);
        fastDFSTemplateFactory.setG_connect_timeout(g_connect_timeout);
        fastDFSTemplateFactory.setG_charset(g_charset);
        fastDFSTemplateFactory.setG_network_timeout(g_network_timeout);
        fastDFSTemplateFactory.setG_secret_key(g_secret_key);
        fastDFSTemplateFactory.setG_anti_steal_token(g_anti_steal_token);
        fastDFSTemplateFactory.setG_tracker_http_port(g_tracker_http_port);
        fastDFSTemplateFactory.setTracker_servers(tracker_server);
        fastDFSTemplateFactory.setSepapator(",");
        fastDFSTemplateFactory.init();
        return fastDFSTemplateFactory;
    }

    @Bean
    public FastDFSTemplate fastDFSTemplate(FastDFSTemplateFactory fastDFSTemplateFactory) {
        return new FastDFSTemplate(fastDFSTemplateFactory);
    }

}
